<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->unsignedBigInteger('user_id')->index()->comment('作者ID');
            $table->string('title')->comment('标题');
            $table->string('slug')->comment('URL标识')->unique();
            $table->longText('content')->comment('内容');
            $table->text('summary')->comment('摘要')->nullable();
            $table->enum('content_type', ['markdown', 'html'])->default('markdown')->comment('内容类型');
            $table->string('cover_image', 500)->nullable()->comment('封面图');
            $table->enum('status', ['draft', 'review', 'published', 'archived'])->default('draft')->comment('状态')->index('status');
            $table->unsignedInteger('view_count')->default(0)->comment('浏览数');
            $table->unsignedInteger('like_count')->default(0)->comment('点赞数');
            $table->unsignedInteger('comment_count')->default(0)->comment('评论数');
            $table->unsignedInteger('favorite_count')->default(0)->comment('收藏数');
            $table->unsignedSmallInteger('reading_time')->default(0)->comment('阅读时长(分钟)');
            $table->timestamp('published_at')->nullable()->comment('发布时间')->index('published_at');
            $table->string('password', 50)->nullable()->comment('密码');
            $table->timestamps();

            $table->index('created_at');
            $table->fulltext(['title', 'content'], 'articles_title_content_index');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->comment('文章表');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('articles');
    }
}
